package com.test1;

import java.util.LinkedList;
import java.util.ListIterator;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Bitmap;
import android.graphics.Rect;

public class GameObject extends BaseObject implements GameListener{
	public Point position = new Point( 0, 0 );
//	public Bitmap bitmap = null;
	public int w = 0;
	public int h = 0;
	public int x = 0;
	public int y = 0;
	public int depth = 0;
	private int ax = 0; //绝对X地址
	private int ay = 0; //绝对Y地址
	private boolean _visible = true;
	public GameLayer father = null;
	private Canvas canvas = null;
	protected float rotation;
	protected int rotationPivotX, rotationPivotY;
	protected boolean rotateAboutPoint;
	private LinkedList< GameObject > children = new LinkedList< GameObject >();
	public GameObject(){
		super();
	}
	
	public final void OnDraw( Canvas canvas, int x, int y ){
		this.canvas = canvas;
		this.ax = x;
		this.ay = y;
		this.draw();
		int size = children.size();
		for( int i = 0; i < size; i++ ){
			GameObject child = children.get( i );
			child.OnDraw( canvas , child.x+x, child.y+y );
		}
		this.canvas = null;
	}
	
	public final void onUpdate( long e ){
		this.update( e );
		int size = children.size();
		for( int i = 0; i < size; i++ ){
			GameObject child = children.get( i );
			child.onUpdate( e );
		}
	}
	
	protected void drawText( GameFont font, String text, int x, int y ){
		if( font == null ){
			this.canvas.drawText( text, this.ax + x, this.ay + y, null );
			return;
		}
		font.drawText( this.canvas, text, this.ax + x, this.ay + y );
	}
	
	protected void drawImage( GameImage img ){
		this.canvas.drawBitmap( img.bitmap, this.ax, this.ay, null);
	}
	
	protected void drawImage( GameImage img, int x, int y ){
		this.canvas.drawBitmap( img.bitmap, this.ax + x, this.ay + y, null);
	}
	
	protected void drawImage( GameImage img, int left, int top, int right, int bottom){
		Rect dst = ResourceManager.popRect();
		dst.left = this.ax + left;
		dst.right = this.ax + right;
		dst.top = this.ay + top;
		dst.bottom = this.ay + bottom;
		this.canvas.drawBitmap( img.bitmap, img.rect, dst, null );
		ResourceManager.pushRect( dst );
	}
	
	public void draw(){
//		canvas
	}
	
	public void update( long e ){
		
	}
	
	public void setPosition( int x, int y ){
		this.x = x;
		this.y = y;
	}
	
	public void setSize( int w, int h ){
		this.w = w;
		this.h = h;
	}
	
//	public int getX(){
//		return this.x;
//	}
//	
//	public int getY(){
//		return this.y;
//	}
//	
//	public int getW(){
//		return this.w;
//	}
//	
//	public int getH(){
//		return this.h;
//	}
	public void setVisible( boolean b ){
		_visible = b;
	}
	
	public boolean getVisible(){
		return _visible;
	}
	
	public float getRotation() {
        return rotation;
    }
	
	public float angle() {
		return rotation;
	}
	
	public void setRotation(float rotation) {
		this.rotation = rotation;
	}
	
	public void setRotation(float rotation, int rotationPivotX, int rotationPivotY) {
		this.rotation = rotation;
		this.rotationPivotX = rotationPivotX;
		this.rotationPivotY = rotationPivotY;
		rotateAboutPoint = true;
	}
	
	public float getRotationPivotX() {
		return rotationPivotX;
	}
	
	public float getRotationPivotY() {
		return rotationPivotY;
	}
	
	public void rotateAboutCentre() {
		rotateAboutPoint = false;
	}
	
	public void rotateAboutPoint(int rotationPivotX, int rotationPivotY) {
		this.rotationPivotX = rotationPivotX;
		this.rotationPivotY = rotationPivotY;
	}
	
	public void rotate(float rotation) {
		this.rotation += rotation;
	}
	
	public boolean isRotateAboutPoint() {
		return rotateAboutPoint;
	}
	
	public void top(){
		this.father.top( this );
	}
	
	public void bottom(){
		this.father.bottom( this );
	}
	
	public void remove(){
		if( this.father != null )
			this.father.remove( this );
	}
	
	public void remove( GameObject child ){
	
	}
	
	public void onPress( GameEvent event ) {
		// TODO Auto-generated method stub
		ListIterator< GameObject > it = children.listIterator();
		int x, y;
		while( it.hasNext() ){
			GameObject obj = it.next();
			if( event.x >= obj.x && event.x < obj.x+obj.w  && event.y >= obj.y && event.y <= obj.y+obj.h){
				x = event.x;
				y = event.y;
				event.x = event.x - obj.x;
				event.y = event.y - obj.y;
				obj.onPress( event );
				event.x = x;
				event.y = y;
			}
		}
	}

	public void onRelease( GameEvent event ) {
		// TODO Auto-generated method stub
		
	}

	public void onClick( GameEvent event ) {
		// TODO Auto-generated method stub
		
	}

	public void onDragBegin( GameEvent event  ) {
		// TODO Auto-generated method stub
		
	}

	public void onDrag( GameEvent event  ) {
		// TODO Auto-generated method stub
		
	}

	public void onDragEnd( GameEvent event  ) {
		// TODO Auto-generated method stub
		
	}
}
